package com.chhuang.ewcrm.ctrl;

import com.chhuang.ewcrm.utils.LengthTextFilter;
import com.jfoenix.controls.JFXButton;
import javafx.beans.property.StringProperty;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.control.TextField;
import javafx.scene.control.TextFormatter;
import javafx.stage.Stage;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URL;
import java.util.ResourceBundle;

/**
 * @author chhuang
 * @create 2020-10-31-15:26
 */
public class LPKeyBoardController implements Initializable {
    private final Logger logger = LoggerFactory.getLogger(LPKeyBoardController.class);

    @FXML
    private TextField lpTextField;
    @FXML
    private JFXButton submitJFXButton;
    @FXML
    private JFXButton delJFXButton;

    private StringProperty lptext;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        lpTextField.setTextFormatter(new TextFormatter<>(new LengthTextFilter(12)));

        submitJFXButton.setOnAction(event -> {
            String text = lpTextField.getText();
            logger.debug("车牌号submitJFXButton action："+text);
            if(StringUtils.isNotEmpty(text)) {
                lptext.setValue(text);
                logger.debug("车牌号lptext value："+lptext.getValue());
                ((Stage) ((Parent) event.getSource()).getScene().getWindow()).close();
            }
        });

        delJFXButton.setOnAction(event -> {
            String text = lpTextField.getText();
            if(StringUtils.isNotEmpty(text)){
                lpTextField.setText(text.substring(0, text.length()-1));
            }
        });
    }

    @FXML
    public void clickKey(ActionEvent event){
        JFXButton button = (JFXButton)event.getSource();
        String text = button.getText();
        lpTextField.setText(lpTextField.getText()+text);
    }

    public void bindLPText(StringProperty lptext){
        this.lptext = lptext;
    }
}
